<script>
    layui.use(['jquery', 'table', 'dropdown', 'form', 'baseAdmin', 'layer'], function () {
        let $ = layui.jquery;
        let form = layui.form;
        let table = layui.table;
        let dropdown = layui.dropdown;
        let baseAdmin = layui.baseAdmin;
        let data = @json($data);
        if (data != null) {
            console.log(data);
            form.val('*', data);
            form.render();
        }
        baseAdmin.init();

        $(document).on("mouseenter mouseleave", ".file-iteme", function (event) {
            if (event.type === "mouseenter") {
                //鼠标悬浮
                $(this).children(".info").fadeIn("fast");
                $(this).children(".handle").fadeIn("fast");
            } else if (event.type === "mouseleave") {
                //鼠标离开
                $(this).children(".info").hide();
                $(this).children(".handle").hide();
            }
        });
        // 删除图片
        $(document).on("click", ".file-iteme .handle .layui-icon-delete", function (event) {
            $(this).parent().parent().parent().remove();

            let field = $(this).parent().parent().parent().data('field');
            let list = $("#uploader-list .file-iteme[data-field=" + field + "]");
            let imgs = [];
            $.each(list, function (i, v) {
                imgs.push($(v).find('input').val());
            })
            $("#uploader-list").parent().parent().prev('input').val(imgs.join(','));
        });
        // 左移动
        $(document).on("click", ".file-iteme .handle .layui-icon-left", function (event) {
            var item = $(this).parent().parent().parent(".file-iteme");
            var item_left = item.prev(".file-iteme");
            if ($("#uploader-list").children(".file-iteme").length >= 2) {
                if (item_left.length == 0) {
                    item.insertAfter($("#uploader-list").children(".file-iteme:last"))
                } else {
                    item.insertBefore(item_left)
                }
            }

            // 设置表单值
            let field = $(this).parent().parent().parent().data('field');
            let list = $("#uploader-list .file-iteme[data-field=" + field + "]");
            let imgs = [];
            $.each(list, function (i, v) {
                imgs.push($(v).find('input').val());
            })
            $("#uploader-list").parent().parent().prev('input').val(imgs.join(','));
        });
        // 右移动
        $(document).on("click", ".file-iteme .handle .layui-icon-right", function (event) {
            var item = $(this).parent().parent().parent(".file-iteme");
            var item_right = item.next(".file-iteme");
            if ($("#uploader-list").children(".file-iteme").length >= 2) {
                if (item_right.length == 0) {
                    item.insertBefore($("#uploader-list").children(".file-iteme:first"))
                } else {
                    item.insertAfter(item_right)
                }
            }

            // 设置表单值
            let field = $(this).parent().parent().parent().data('field');
            let list = $("#uploader-list .file-iteme[data-field=" + field + "]");
            let imgs = [];
            $.each(list, function (i, v) {
                imgs.push($(v).find('input').val());
            })
            $("#uploader-list").parent().parent().prev('input').val(imgs.join(','));
        });

        // 新增/编辑保存表单提交
        form.on('submit(save)', function (data) {
            var postData = data.field;

            var uri = $(data.elem).attr('request-uri');
            $.ajax({
                url: uri,
                data: JSON.stringify(postData),
                dataType: 'json',
                contentType: 'application/json',
                type: 'post',
                success: function (result) {
                    if (result.code == 200) {
                        if (postData.id == 0) {
                            // 判断是否继续新增
                            layer.confirm('是否继续新增', function (index) {
                                self.location.reload();
                                layer.close(index);
                            }, function () {
                                parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
                                parent.tableReload();
                            });
                        } else {
                            // 直接关闭窗口
                            layer.msg(result.message, {icon: 1, time: 1000}, function () {
                                parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页parent.tableReload();
                            });
                        }
                    } else {
                        layer.msg(result.message, {icon: 2, time: 1000});
                    }
                }, error: function (e) {
                    console.log(e);
                    layer.msg(e.responseJSON.message, {icon: 2, time: 1000});
                }
            });
            return false;
        });
    })
</script>
